package com.fanketly.funacg2.ui.components

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CornerBasedShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Snooze
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import com.fanketly.funacg2.R
import com.fanketly.funacg2.ui.theme.FunACG2Theme
import com.fanketly.funacg2.utils.TimeUtils.timeConversion
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay


/**
 * 进度条
 * */
@Composable
fun AudioProgressBar(
    progress: Float,
    modifier: Modifier = Modifier,
    shape: CornerBasedShape = MaterialTheme.shapes.small,
    barBackgroundColor: Color = MaterialTheme.colorScheme.primaryContainer,
    barRoundColor: Color = MaterialTheme.colorScheme.onPrimaryContainer,
    barColor: Color = MaterialTheme.colorScheme.primary,
    onPress: (Offset) -> Unit,
    onDragStarted: suspend CoroutineScope.(startedPosition: Offset) -> Unit = {},
    onDragStopped: suspend CoroutineScope.(velocity: Float) -> Unit = {},
    onDelta: (Float) -> Unit,
) {
    Surface(
        shape = shape,
        modifier = modifier
            .fillMaxWidth()
            .height(16.dp)
            .draggable(
                orientation = Orientation.Horizontal,
                state = rememberDraggableState(onDelta = onDelta),
                onDragStarted = onDragStarted,
                onDragStopped = onDragStopped
            )
            .pointerInput(Unit) {
                detectTapGestures(onPress = { onPress(it) })
            },
        color = barBackgroundColor
    ) {
        Row {
            Surface(
                modifier = Modifier
                    .fillMaxHeight()
                    .fillMaxWidth(progress), color = barColor
            ) {
            }
            Divider(
                modifier = Modifier
                    .fillMaxHeight()
                    .fillMaxWidth(0.02f), color = barRoundColor
            )
        }
    }
}

@Composable
fun AudioProgressBarTime(
    nowTime: String,
    allTime: String,
    modifier: Modifier = Modifier,
    textColor: Color = Color.Unspecified,
    fontSize: TextUnit = TextUnit.Unspecified,
    style: TextStyle = MaterialTheme.typography.bodySmall,
) {
    Row(modifier = modifier.fillMaxWidth(0.9f)) {
        Text(text = nowTime, color = textColor, fontSize = fontSize, style = style)
        Spacer(modifier = Modifier.weight(1f))
        Text(text = allTime, color = textColor, fontSize = fontSize, style = style)
    }
}


@Composable
fun MiGuAudioProgressBar(
    progress: Float,
    height: Dp,
    modifier: Modifier = Modifier,
    shape: CornerBasedShape,
    barBackgroundColor: Color = MaterialTheme.colorScheme.primaryContainer,
    barRoundColor: Color = MaterialTheme.colorScheme.onPrimaryContainer,
    barColor: Color = MaterialTheme.colorScheme.primary,
//    onDragStarted: suspend CoroutineScope.(startedPosition: Offset) -> Unit = {},
//    onDragStopped: suspend CoroutineScope.(velocity: Float) -> Unit = {},
//    onPress: (Offset) -> Unit,
//    onDelta: (Float) -> Unit,
) {
    Surface(
        shape = shape,
        modifier = modifier
            .padding(start = height.div(2))
            .fillMaxWidth()
            .height(height)
        /* .draggable(
             orientation = Orientation.Horizontal,
             state = rememberDraggableState(onDelta = onDelta),
             onDragStarted = onDragStarted,
             onDragStopped = onDragStopped
         )
         .pointerInput(Unit) {
             detectTapGestures(onPress = { onPress(it) })
         }*/,
        color = barBackgroundColor
    ) {
        Row {
            Surface(
                modifier = Modifier
                    .fillMaxHeight()
                    .fillMaxWidth(progress),
                color = barColor,
                shape = RoundedCornerShape(bottomEnd = 25.dp, topEnd = 25.dp)
            ) {
            }
//            Divider(
//                modifier = Modifier
//                    .fillMaxHeight()
//                    .fillMaxWidth(0.02f), color = barRoundColor
//            )
        }
    }
}

@Preview(showBackground = true, device = Devices.PIXEL)
@Composable
private fun MiGuPlayProgressPreview() {
    FunACG2Theme {
        Surface(Modifier.fillMaxWidth()) {
            Box(
                modifier = Modifier
                    .fillMaxWidth(0.8f)
                    .height(100.dp)
                    .padding(4.dp), contentAlignment = Alignment.CenterStart
            ) {

                MiGuAudioProgressBar(
                    0.5f,
                    50.dp,
                    shape = MaterialTheme.shapes.extraLarge,
                )

                Image(
                    painter = painterResource(id = R.drawable.ic_cd),
                    contentDescription = null,
                    modifier = Modifier
                        .size(50.dp)
                        .clip(CircleShape)
                )
                Row(modifier = Modifier.align(Alignment.CenterEnd)) {
                    PlayOrPauseIconButton(isPlaying = false, tint = Color.Black) {

                    }
                    PlayListIconButton(tint = MaterialTheme.colorScheme.onPrimaryContainer) {

                    }
                }
            }

        }
    }
}

@Composable
private fun Preview() {
//    AudioDetail()
    var time by remember {
        mutableStateOf(1000)
    }
    val rememberCoroutineScope = rememberCoroutineScope()
    FunACG2Theme(true) {
        Row(
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            androidx.compose.material3.IconButton(//播放列表
                modifier = Modifier.weight(1f),
                onClick = {}
            ) {
                androidx.compose.material3.Icon(
                    Icons.Filled.Favorite,
                    contentDescription = null,
                    modifier = Modifier
                        .padding(top = 6.dp)
                        .fillMaxSize(0.7f),
                    tint = Color.Gray
                )
            }
            Column(
                modifier = Modifier
//                    .padding(8.dp)
                    .fillMaxWidth(0.7f),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                val s = 2819
                AudioProgressBarTime(
                    timeConversion(time),
                    timeConversion(s),
                    style = MaterialTheme.typography.bodySmall
                )
                AudioProgressBar(time.toFloat() / s, onPress = {}, onDelta = {})

                LaunchedEffect(key1 = time, block = {
//                while (time < 2819) {
                    if (time < 2819) {
                        delay(1000)
                        time += 1
                        Log.i("TAG", "Preview: $time")
                    }
                })
            }
            androidx.compose.material3.IconButton(//播放列表
                modifier = Modifier.weight(1f),
                onClick = {}
            ) {
                androidx.compose.material3.Icon(
                    Icons.Filled.Snooze,
                    contentDescription = null,
                    modifier = Modifier
                        .padding(top = 6.dp)
                        .fillMaxSize(0.7f),
                    tint = Color.Gray
                )
            }
        }
    }
}